var $ = require("../../utils/Ajax.js");
Page({
  data: {
    PictureUrl: getApp().PictureUrl,
    Goods: null,
    Cause: "请选择",

    // 底部所有目录控制
    showShopPopup: false,
    animationData: {},
    BarterCont: ["换货"]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "仅换货"
    })
    this.setData({
      Id: JSON.parse(options.Goods).id,
      Goods: JSON.parse(options.Goods)
    })
  },

  // 点击选择换货原因
  SelectBarter: function () {
    var animation = wx.createAnimation({
      duration: 400,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(658).step()
    this.setData({
      animationData: animation.export(),
      showShopPopup: true
    })
    setTimeout(() => {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }, 200)
  },

  // 选择换货原因
  Cause: function (e) {
    var Cause = e.detail.value;
    this.setData({
      Cause: Cause
    })
  },

  // 填写退款说明
  Explain: function (e) {
    var Explain = e.detail.value;
    this.setData({
      Explain: Explain
    })
  },

  // 关闭换货原因
  BarterBgClose: function (e) {
    var Status = e.currentTarget.dataset.status;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(658).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
    if (Status != 1) {
      this.setData({
        Cause: '请选择'
      })
    }
  },

  // 提交换货原因
  Barter: function(){
    var that = this;
    var OrderId = that.data.Id;
    var Cause = that.data.Cause;
    var Explain = that.data.Explain
    if (Cause == '请选择') {
      wx.showToast({
        title: '请选择退货/退款原因',
        icon: 'none',
        duration: 1500
      })
    } else {
      $.Ajax("api/refund", "POST", {
        orderId: OrderId,
        type: 1,
        cause: Cause,
        explain: Explain
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000);
      })
    }
  }
})